local japi = require 'jass.japi'
local jass = require 'jass.common'



local mt = extends(class.ui_base){}

--类型
mt.type = 'panel'

--魔兽控件类型
mt._type = 'BACKDROP'

--识别名称
mt._name = '面板类'

--模板
mt.fdf = 'panel'

--图标路径
mt.normal_image = nil  

--- 存放所有panel
mt.allpanels = {}

--- 转换
function mt.convert(handle, parent, is_default)
    local control = mt.allpanels[handle]
    if control then 
        return control
    end 
    control = class.ui_base.convert(handle, 'panel', parent, is_default)
    mt.allpanels[handle] = control
    return control
end

--- 构造
function mt.new(parent, image_path ,x, y , w, h)
    local control = class.panel:builder
    {
        parent = parent,
        normal_image = image_path,
        x = x,
        y = y,
        w = w,
        h = h,
        is_scroll = scroll,
    }
    return control
end 


--- 初始化
function mt:init()
    if self.handle then return end
    class.ui_base.init(self)
    --BACKDROP        panel[4]        610140296       panel   4
    local handle = japi.CreateFrameByTagName( self._type, self._name, self.parent.handle, self.fdf, self._index )
    if not handle or handle == 0 then
        class.ui_base.remove(self)
        log.error('创建panel失败')
        return
    end
    self.handle = handle
    self.allpanels[handle] = self
    class.ui_base.allframes[handle] = self 
    if self.fdf==mt.fdf then
        self:set_normal_image(self.normal_image)
    end
    self:set_alpha(self.alpha)
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    if self.view_port then 
        self:set_view_port(self.view_port)
    end
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    return self
end

---删除控件
function mt:remove()
    if self.removed then return end
    if self:is_simple() then return end
    self.allpanels[self.handle] = nil
    class.ui_base.remove(self)
end

---设置图像
---@param normal_image string 图像路径
---@param flag number 平铺
function mt:set_normal_image(normal_image,flag)
    if self.removed then return end 
    self.normal_image = normal_image
    if normal_image==nil or normal_image == '' then
        japi.FrameSetTexture(self.handle,'core\\transparent.tga',flag or 0)
    else
        japi.FrameSetTexture(self.handle,normal_image,flag or 0)
    end
end

---获取图像
---@return string texture
function mt:get_normal_image()
    return self.normal_image
end


class.panel = mt

return panel